From 2c089cf6dda04854e5bbf38ce9f24335eb745908 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 7 Mar 2016 21:51:44 -0800 Subject: [PATCH] Fix rerunning rustdoc when output deleted If `crate/index.html` is missing, we need to rerun rustdoc! Closes #2379 --- src/cargo/ops/cargo_rustc/context.rs | 6 +++++- src/cargo/ops/cargo_rustc/fingerprint.rs | 5 ++++- src/cargo/ops/cargo_rustc/layout.rs | 6 ++++++ src/cargo/ops/cargo_rustc/mod.rs | 5 +---- tests/test_cargo_doc.rs | 24 ++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 8d88ac384..b350ba6ef 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -213,7 +213,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> { /// Returns the appropriate output directory for the specified package and /// target. pub fn out_dir(&self, unit: &Unit) -> PathBuf { - self.layout(unit.pkg, unit.kind).out_dir(unit.pkg, unit.target) + if unit.profile.doc { + self.layout(unit.pkg, unit.kind).doc_root() + } else { + self.layout(unit.pkg, unit.kind).out_dir(unit.pkg, unit.target) + } } /// Return the (prefix, suffix) pair for dynamic libraries. diff --git a/src/cargo/ops/cargo_rustc/fingerprint.rs b/src/cargo/ops/cargo_rustc/fingerprint.rs index d0007f52d..837b14bc1 100644 --- a/src/cargo/ops/cargo_rustc/fingerprint.rs +++ b/src/cargo/ops/cargo_rustc/fingerprint.rs @@ -59,7 +59,10 @@ pub fn prepare_target<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, let root = cx.out_dir(unit); let mut missing_outputs = false; - if !unit.profile.doc { + if unit.profile.doc { + missing_outputs = !root.join(unit.target.crate_name()) + .join("index.html").exists(); + } else { for filename in try!(cx.target_filenames(unit)).iter() { missing_outputs |= fs::metadata(root.join(filename)).is_err(); } diff --git a/src/cargo/ops/cargo_rustc/layout.rs b/src/cargo/ops/cargo_rustc/layout.rs index d25a8199d..4add3a758 100644 --- a/src/cargo/ops/cargo_rustc/layout.rs +++ b/src/cargo/ops/cargo_rustc/layout.rs @@ -165,4 +165,10 @@ impl<'a> LayoutProxy<'a> { self.root().to_path_buf() } } + + pub fn doc_root(&self) -> PathBuf { + // the "root" directory ends in 'debug' or 'release', and we want it to + // end in 'doc' instead + self.root.root().parent().unwrap().join("doc") + } } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index b71da0fcc..dad110c20 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -374,10 +374,7 @@ fn rustdoc(cx: &mut Context, unit: &Unit) -> CargoResult { rustdoc.arg("--target").arg(target); } - // the "root" directory ends in 'debug' or 'release', and we want it to end - // in 'doc' instead - let doc_dir = cx.layout(unit.pkg, unit.kind).proxy().root(); - let doc_dir = doc_dir.parent().unwrap().join("doc"); + let doc_dir = cx.out_dir(unit); // Create the documentation directory ahead of time as rustdoc currently has // a bug where concurrent invocations will race to create this directory if diff --git a/tests/test_cargo_doc.rs b/tests/test_cargo_doc.rs index a53a62120..6bc412b43 100644 --- a/tests/test_cargo_doc.rs +++ b/tests/test_cargo_doc.rs @@ -1,4 +1,5 @@ use std::str; +use std::fs; use support::{project, execs, path2url}; use support::{COMPILING, DOCUMENTING, RUNNING}; @@ -525,3 +526,26 @@ test!(features { assert_that(&p.root().join("target/doc/foo/fn.foo.html"), existing_file()); assert_that(&p.root().join("target/doc/bar/fn.bar.html"), existing_file()); }); + +test!(rerun_when_dir_removed { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", r#" + /// dox + pub fn foo() {} + "#); + assert_that(p.cargo_process("doc"), + execs().with_status(0)); + assert_that(&p.root().join("target/doc/foo/index.html"), existing_file()); + + fs::remove_dir_all(p.root().join("target/doc/foo")).unwrap(); + + assert_that(p.cargo_process("doc"), + execs().with_status(0)); + assert_that(&p.root().join("target/doc/foo/index.html"), existing_file()); +}); -- 2.30.2